package main

import (
	"context"
	"github.com/micro/go-micro/v2"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-micro/v2/registry/etcd"
	pb "go-micro-demo/proto"
	"log"
)

// 实现Server API for UserService service
type UserService struct {
}

func (s *UserService) Test(ctx context.Context, req *pb.UserRequest, resp *pb.UserResponse) error {
	resp.Ret = "users" + req.Id
	return nil
}

func NewUserService() *UserService {
	return &UserService{}
}

func main() {
	// 创建服务对象实例
	service := micro.NewService(
		micro.Name("go.micro.yjw.user"),
		micro.Version("v1.0.0"),
		micro.Registry(etcd.NewRegistry(
			registry.Addrs("139.155.239.206:2379"),
		)),
	)

	// 服务初始化
	service.Init()

	// 注册服务
	// 第二个参数传入UserService指针就行
	err := pb.RegisterUserServiceHandler(service.Server(), NewUserService())
	//err := pb.RegisterUserServiceHandler(service.Server(), new(UserService))
	if err != nil {
		log.Fatal(err)
	}

	// 启动服务
	if err = service.Run(); err != nil {
		log.Fatal(err)
	}
}
